package com.ruoyi.TaskForce.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.TaskForce.domain.Team_Members;
import com.ruoyi.TaskForce.mapper.AreaNameManagementMapper;
import com.ruoyi.TaskForce.mapper.TeammateAllocationManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.TaskForce.mapper.TeamManagementMapper;
import com.ruoyi.TaskForce.domain.TeamManagement;
import com.ruoyi.TaskForce.service.ITeamManagementService;

/**
 * TaskForceService业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-09
 */
@Service
public class TeamManagementServiceImpl implements ITeamManagementService 
{
    @Autowired
    private TeamManagementMapper teamManagementMapper;

    @Autowired
    private TeammateAllocationManagementMapper teammateAllocationManagementMapper;

    @Autowired
    private AreaNameManagementMapper areaNameManagementMapper;

    /**
     * 查询TaskForce
     * 
     * @param id TaskForce主键
     * @return TaskForce
     */
    @Override
    public TeamManagement selectTeamManagementById(Long id)
    {
        return teamManagementMapper.selectTeamManagementById(id);
    }

    /**
     * 查询TaskForce列表
     * 
     * @param teamManagement TaskForce
     * @return TaskForce
     */
    @Override
    public List<TeamManagement> selectTeamManagementList(TeamManagement teamManagement)
    {
        return teamManagementMapper.selectTeamManagementList(teamManagement);
    }

    /**
     * 新增TaskForce
     * 
     * @param teamManagement TaskForce
     * @return 结果
     */
    @Override
    public int insertTeamManagement(TeamManagement teamManagement)
    {
        return teamManagementMapper.insertTeamManagement(teamManagement);
    }

    /**
     * 修改TaskForce
     * 
     * @param teamManagement TaskForce
     * @return 结果
     */
    @Override
    public int updateTeamManagement(TeamManagement teamManagement)
    {
        return teamManagementMapper.updateTeamManagement(teamManagement);
    }

    /**
     * 批量删除TaskForce
     * 
     * @param ids 需要删除的TaskForce主键
     * @return 结果
     */
    @Override
    public int deleteTeamManagementByIds(Long[] ids)
    {
        return teamManagementMapper.deleteTeamManagementByIds(ids);
    }

    /**
     * 删除TaskForce信息
     * 
     * @param id TaskForce主键
     * @return 结果
     */
    @Override
    public int deleteTeamManagementById(Long id)
    {
        return teamManagementMapper.deleteTeamManagementById(id);
    }

    @Override
    public List<Team_Members> getTeamsByTaskForceId(Long id) {

        return teammateAllocationManagementMapper.getTeamsByTaskForceId(id);
    }

    @Override
    public int insertTeamMembers(List<Team_Members> teamMembers) {
        // 过滤出需要更新的队员（有ID）
        List<Team_Members> updateList = teamMembers.stream()
                .filter(member -> member.getId() != null)
                .collect(Collectors.toList());

        // 过滤出需要新增的队员（无ID）
        List<Team_Members> insertList = teamMembers.stream()
                .filter(member -> member.getId() == null)
                .collect(Collectors.toList());

        int rowsAffected = 0;

        // 执行更新操作
        if (!updateList.isEmpty()) {
            for (Team_Members member : updateList) {
                rowsAffected += teammateAllocationManagementMapper.updateTeamMembers(member);
            }
            }
        // 执行插入操作
        if (!insertList.isEmpty()) {
            for (Team_Members members : insertList){
                rowsAffected += teammateAllocationManagementMapper.insertTeamMembers(members);

            }

        }

        return rowsAffected; // 返回受影响的行数
    }

    @Override
    public int DeleteTeamMembers(Long id) {
        return teammateAllocationManagementMapper.DeleteTeamMembers(id);
    }

    @Override
    public int SaveImage(Long id, String idCardImagePath) {
        return teammateAllocationManagementMapper.SaveImage(id, idCardImagePath);
    }

    @Override
    public Team_Members GetImageData(Long id) {
        return teammateAllocationManagementMapper.GetImageData(id);
    }

    @Override
    public List<String> selectAreaName() {
        return areaNameManagementMapper.selectAreaName();
    }

    @Override
    public int saveCaptainInfo(String phone_number, String name, Long team_id) {
        return teamManagementMapper.saveCaptainInfo(phone_number, name, team_id);
    }


}
